/*
 * @Description: 统一请求处理后端article请求
 * @Author: YWJ 
 * @Date: 2022-04-20 18:07:53 
 * @Last Modified by: YWJ
 * @Last Modified time: 2022-05-15 23:06:26
 */

import request from '@/request'


export function getArticles(params) {
  console.log(params)
	return request({
		  method: 'get',
		  url: '/index/list',
		  params: params
		})
}

export function getLikeMostArticles(params) {
	return request({
		  method: 'get',
		  url: '/index/list-like',
		  params: params
		})
}

export function getPersonalArticles(params) {
	return request({
		  method: 'get',
		  url: '/user/my-article',
		  params: params,
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      }
		})
}



export function getLikeArticles(data) {
	return request({
		  method: 'get',
		  url: '/user/list-like',
		  params: data,
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      }
		})
}

export function getCollectArticles(data) {
	return request({
		  method: 'get',
		  url: '/user/list-collect',
		  params: data,
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      }
		})
}

export function getHotArticles() {
    return request({
      url: '/articles/hot',
      method: 'post'
    })
  }
  
  export function getNewArticles() {
    return request({
      url: '/articles/new',
      method: 'post'
    })
  }

  export function getArchives() {
    return request({
      url: '/articles/listArchives',
      method: 'post'
    })
  }


//   请求具体文章详情
  export function findArticleById(id,account) {
    return request({
      url: '/index/get-article',
      method: 'get',
      params:{
        aid:id,
        account:account
      }
    })
  }


  export function publishPicture(pic) {
    return request({
      url: '/user/publish-picture',
      method: 'post',
      data: pic
    })
  }

  export function publishArticle(article) {
    return request({
      url: '/user/publish-article',
      method: 'post',
      data: article,
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      },
    })
  }

  export function deleteArticleById(id) {
    return request({
      url: '/article/delete-article',
      method: 'get',
      params:{
        aid:id
      },
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      }
    })
  }

  export function updateArticle(article) {
    return request({
      url: '/article/update-article',
      method: 'post',
      data: article,
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      },
    })
  }


  export function like(account,aid ,isCollect) {
    return request({
      url: '/article/like-article',
      method: 'get',
      params:{
        account:account,
        aid:aid,
        isCollect:isCollect
      },
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      }
    })
  }

  export function dislike(account,aid,isCollect) {
    return request({
      url: '/article/delete-like',
      method: 'get',
      params:{
        account:account,
        aid:aid,
        isCollect:isCollect
      },
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      }
    })
  }

  export function collect(account,aid,isLike) {
    return request({
      url: '/article/collect-article',
      method: 'get',
      params:{
        account:account,
        aid:aid,
        isLike:isLike
      },
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      }
    })
  }

  export function discollect(account,aid,isLike) {
    return request({
      url: '/article/delete-collect',
      method: 'get',
      params:{
        account:account,
        aid:aid,
        isLike:isLike
      },
      headers: {
        ContentType: "application/json",
        token: localStorage.getItem("token"),
      }
    })
  }

  
  export function getSearchArticlesTitle(params) {
    return request({
      url: '/index/search-title',
      method: 'post',
      params: params,
      headers: {
        ContentType: "application/json"
      },
    })
  }

  export function getSearchArticlesContext(params) {
    return request({
      url: '/index/search-context',
      method: 'post',
      params: params,
      headers: {
        ContentType: "application/json"
      },
    })
  }

  export function getSearchArticlesAuthor(params) {
    return request({
      url: '/index/search-user',
      method: 'post',
      params: params,
      headers: {
        ContentType: "application/json"
      },
    })
  }

  export function getHotTag() {
    return request({
      url: '/index/list-tag',
      method: 'get'
    })
  }

  export function getUserInfo(params) {
    return request({
      url: '/user-center/get-info',
      method: 'post',
      params: params,
      headers: {
        ContentType: "application/json",
        token:localStorage.getItem("token")
      },
    })
  }

  export function checkFocus(account,followAccount) {
    return request({
      url: '/follow/check-follow',
      method: 'get',
      params: {
        account:account,
        followAccount:followAccount
      },
      headers: {
        ContentType: "application/json",
        token:localStorage.getItem("token")
      },
    })
  }

  export function Focus(account,followAccount) {
    return request({
      url: '/follow/add-follow',
      method: 'get',
      params: {
        account:account,
        followAccount:followAccount
      },
      headers: {
        ContentType: "application/json",
        token:localStorage.getItem("token")
      },
    })
  }

  export function unFocus(account,followAccount) {
    return request({
      url: '/follow/delete-follow',
      method: 'get',
      params: {
        account:account,
        followAccount:followAccount
      },
      headers: {
        ContentType: "application/json",
        token:localStorage.getItem("token")
      },
    })
  }

  export function listFocusAuthor() {
    return request({
      url: '/user-center/list-follow',
      method: 'post',
      params: {
        account:localStorage.getItem("account"),
      },
      headers: {
        ContentType: "application/json",
        token:localStorage.getItem("token")
      },
    })
  }

  export function getFocusArticles(data) {
    return request({
        method: 'post',
        url: '/user-center/list-article',
        params: data,
        headers: {
          ContentType: "application/json",
          token: localStorage.getItem("token"),
        }
      })
  }

  let timer, flag;
  export function throttle(func, wait = 500, immediate = true) {
	if (immediate) {
		if (!flag) {
			flag = true;
			// 如果是立即执行，则在wait毫秒内开始时执行
			typeof func === 'function' && func();
			timer = setTimeout(() => {
				flag = false;
			}, wait);
		}
	} else {
		if (!flag) {
			flag = true
			// 如果是非立即执行，则在wait毫秒内的结束处执行
			timer = setTimeout(() => {
				flag = false
				typeof func === 'function' && func();
			}, wait);
		}
		
	}
};
